package com.example.joke.custom.progress

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.example.joke.R

class ProgressBarView : View {

    private var mBtmColor = Color.GREEN
    private var mTopColor = Color.RED
    private var mBorderWidth = 20   // 20px
    private var mTextSize = 30
    private var mTextColor = Color.BLACK
    private var mPaintBtm : Paint
    private var mPaintTop : Paint
    private var mPaintText : Paint
    // 最大步数
    var mStepMax = 0
    // 当前步数
    var mCurrentQQStep = 0
        set(value) {
            field = value
            // 对于值得每一次改变，调用重绘，所以形成动画效果
            invalidate()
        }
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val typedArray = context?.obtainStyledAttributes(attrs, R.styleable.QQStepView)
        mBtmColor = typedArray?.getColor(R.styleable.QQStepView_btmColor, mBtmColor)!!
        mTopColor = typedArray.getColor(R.styleable.QQStepView_topColor, mTopColor)!!
        mBorderWidth =
            typedArray.getDimension(R.styleable.QQStepView_borderWidth, mBorderWidth.toFloat()).toInt()!!
        mTextSize = typedArray.getDimensionPixelSize(R.styleable.QQStepView_stepTextSize, mTextSize)
        mTextColor = typedArray.getColor(R.styleable.QQStepView_stepTextColor, mTextColor)
        typedArray.recycle()

        mPaintBtm = Paint()
        mPaintBtm.isAntiAlias = true
        mPaintBtm.color = mBtmColor
        mPaintBtm.strokeWidth = mBorderWidth.toFloat()
        mPaintBtm.style = Paint.Style.STROKE
//        mPaintBtm.strokeCap = Paint.Cap.ROUND

        mPaintTop = Paint()
        mPaintTop.isAntiAlias = true
        mPaintTop.color = mTopColor
        mPaintTop.strokeWidth = mBorderWidth.toFloat()
        mPaintTop.style = Paint.Style.STROKE
//        mPaintTop.strokeCap = Paint.Cap.ROUND

        mPaintText = Paint()
        mPaintText.isAntiAlias = true
        mPaintText.color = mTextColor
        mPaintText.textSize = mTextSize.toFloat()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)

        setMeasuredDimension(if(width > height) height else width, if(width > height) height else width)
    }

    override fun onDraw(canvas: Canvas?) {
        // 画底部的圆
        val center = (width / 2).toFloat()
        canvas?.drawCircle(center, center, center - mBorderWidth / 2, mPaintBtm)
        // 画顶部进度的圆
        val rect = RectF(
            (mBorderWidth / 2).toFloat(), (mBorderWidth / 2).toFloat(),
            (width - mBorderWidth / 2).toFloat(), (height - mBorderWidth / 2).toFloat()
        )
        val sweep = mCurrentQQStep.toFloat() / mStepMax * 360
        canvas?.drawArc(rect, 0f, sweep, false, mPaintTop)
        // 画文字
        val text = (mCurrentQQStep.toFloat() / mStepMax * 100).toInt().toString().plus("%")
        val textBounds = Rect()
        mPaintText.getTextBounds(text, 0, text.length, textBounds)
        val x = width / 2 - textBounds.width() / 2
        val fontMetrics = mPaintText.fontMetricsInt
        val dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
        val baseLine = height / 2 + dy
        canvas?.drawText(text, x.toFloat(), baseLine.toFloat(), mPaintText)
    }
}